package com.app.community.util;

import com.aliyun.oss.OSS;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 *  阿里云OSS上传工具
 *  2020/10/18 21:42
 **/
public class AliyunOSSUtils {

    private final String bucketName = "hgshequ-bucket";

    // 获取当前日期
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String date = formatter.format(Calendar.getInstance().getTime());

    /**
     * 上传问题图片
     */
    public String uploadQuestionImgToOSS(OSS ossClient,InputStream inputStream,Long questionId,String fileName){
        // <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg。
        String objectName = "question/" + questionId + "/" + fileName;
        // 上传到OSS
        ossClient.putObject(bucketName, objectName, inputStream);

        // 图片路径
        return "https://hgshequ-bucket.oss-cn-shanghai.aliyuncs.com/" + objectName;
    }

    /**
     * 上传用户图片
     */
    public String uploadUserImgToOSS(OSS ossClient,InputStream inputStream,String userId,String fileNmae){
        String objectName = "user/" + userId + "/" + date + "/" +  fileNmae;
        // 上传到OSS
        ossClient.putObject(bucketName, objectName, inputStream);

        // 图片路径
        return "https://hgshequ-bucket.oss-cn-shanghai.aliyuncs.com/" + objectName;
    }

}
